package our.pack;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DatabaseHelper extends SQLiteOpenHelper{
	
	private final static String DB_NAME = "trackerdb6.db";
	private final static int DB_VERSION = 7; 
	
	private static final String trackTable = "tracks";
	private static final String trackID = "trackID";
	private static final String trackName = "trackName";
	private static final String trackDate = "trackDate";
	private static final String trackDistance = "trackDistance";
	private static final String trackPoints = "points";
	
	private static final String pointsTable = "points";
	private static final String pointsID = "pointsID";
	private static final String pointsLat = "pointsLat";
	private static final String pointsLong = "pointsLong";

	private static final String metaDataTable = "metaDataTable";
	private static final String metaDataName = "metaDataName";
	private static final String metaDataPath = "metaDataPath";
	private static final String metaDataDate = "metaDataDate";
	private static final String metaDataType = "metaDataType";
	private static final String metaDataID = "metaDataID";

	public DatabaseHelper(Context context) {
		super(context, DB_NAME, null, DB_VERSION );
		// TODO Auto-generated constructor stub
	}

	/**
	 * maakt de tabellen van de database aan, er zijn tabellen voor tracks, punten en metadata. 
	 * Een punt heeft als foreign key een trackId
	 * Een metadata heeft als foreign key een pointsId
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		db.execSQL("CREATE TABLE " + getTrackTable() + "(" + getTrackID() + " INTEGER PRIMARY KEY AUTOINCREMENT, " + getTrackName() +" TEXT, " + getTrackDate() + " TEXT, " + getTrackDistance() + " TEXT);");
		
		db.execSQL("CREATE TABLE " + pointsTable + "(" + pointsID + " INTEGER PRIMARY KEY AUTOINCREMENT, " 
				+ pointsLat + " INTEGER, " 
				+ pointsLong + " INTEGER, " 
				+ getTrackID() + " INTEGER NOT NULL, FOREIGN KEY ("+ trackID + ") REFERENCES "+ trackTable + "(" + trackID + "));");
		
		db.execSQL("CREATE TABLE " + metaDataTable + "(" + metaDataID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
				+ metaDataName + " TEXT, "
				+ metaDataPath + " TEXT, "
				+ metaDataDate + " TEXT, "
				+ metaDataType + " TEXT, "
				+ pointsID + " INTEGER NOT NULL, FOREIGN KEY (" + pointsID + ") REFERENCES " + pointsTable + "(" + pointsID + "));");

	}

	

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldNmbr, int newNmbr) {
		// TODO Auto-generated method stub
		db.execSQL("DROP TABLE IF EXISTS " + getTrackTable() );
		db.execSQL("DROP TABLE IF EXISTS " + pointsTable );
		db.execSQL("DROP TABLE IF EXISTS " + metaDataTable);
		onCreate(db);
		
	}

	public static String getTrackID() {
		return trackID;
	}

	public static String getTrackName() {
		return trackName;
	}

	public static String getTrackTable() {
		return trackTable;
	}

	public static String getTrackPoints() {
		return trackPoints;
	}
	
	public static String getPointsID() {
		return pointsID;
	}

	public static String getPointsLat() {
		return pointsLat;
	}

	public static String getPointsLong() {
		return pointsLong;
	}

	public static String getPointsTable() {
		return pointsTable;
	}
	
	public static String getTrackDate(){
		return trackDate;
	}
	
	public static String getTrackDistance() {
		return trackDistance;
	}	
	
	public static String getMetadatatable() {
		return metaDataTable;
	}

	public static String getMetadataname() {
		return metaDataName;
	}

	public static String getMetadatapath() {
		return metaDataPath;
	}

	public static String getMetadatadate() {
		return metaDataDate;
	}

	public static String getMetadatatype() {
		return metaDataType;
	}

	public static String getMetadataid() {
		return metaDataID;
	}

}
